package com.zkthink.ceres.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.zkthink.base.entity.SuperEntity;
import com.zkthink.ceres.product.entity.ProductContributionConfig;
import com.zkthink.ceres.product.entity.ProductGroup;
import com.zkthink.ceres.product.entity.ShippingCompany;
import com.zkthink.ceres.product.service.ProductGroupService;
import com.zkthink.ceres.product.service.ShippingCompanyService;
import com.zkthink.ceres.product.vo.*;
import com.zkthink.log.annotation.SysLog;
import com.zkthink.ceres.product.dto.*;
import com.zkthink.ceres.product.entity.Product;
import com.zkthink.ceres.product.service.ProductService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.zkthink.base.controller.SuperController;
import com.zkthink.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.zkthink.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 商品表
 * </p>
 *
 * @author JustArgo
 * @date 2020-05-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/product")
@Api(value = "Product", tags = "商品表")
//@PreAuth(replace = "product:")
public class ProductController /*extends SuperController<ProductService, Long, Product, ProductPageDTO, ProductSaveDTO, ProductUpdateDTO>*/ {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductGroupService productGroupService;

    @Autowired
    private ShippingCompanyService shippingCompanyService;

    /**
     * Excel导入后的操作
     *
     * @param
     */
    /*@Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Product> productList = list.stream().map((map) -> {
            Product product = Product.builder().build();
            //TODO 请在这里完成转换
            return product;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(productList));
    }*/
    @ApiOperation(
            value = "查询商品详细信息",
            notes = "后台编辑时, 调用本接口"
    )
    @GetMapping({"/query/{id}"})
    @SysLog("'查询:' + #id")
    @PreAuth("hasPermit('{}view')")
    public R<ProductCombineDTO> queryById(@PathVariable Long id) {
        return productService.queryById(id);
    }

    @ApiOperation("新增商品")
    @PostMapping("/save")
    public R<Boolean> saveProduct(@RequestBody ProductCombineDTO dto) {
        return productService.saveProduct(dto);
    }

    @ApiOperation("修改商品")
    @PostMapping("/update")
    public R<Boolean> updateProduct(@RequestBody ProductCombineDTO dto) {
        return productService.updateProduct(dto);
    }

    @ApiOperation("上架商品")
    @PostMapping("/online")
    public R<Boolean> onlineProduct(@RequestBody ProductUpdateDTO dto) {
        return productService.onlineProduct(dto);
    }

    @ApiOperation("下架商品")
    @PostMapping("/offline")
    public R<Boolean> offlineProduct(@RequestBody ProductUpdateDTO dto) {
        return productService.offlineProduct(dto);
    }

    @ApiOperation("逻辑删除商品")
    @PostMapping("/delProduct")
    public R<Boolean> delProduct(@RequestBody IdQueryDTO dto) {
        return productService.delProduct(dto);
    }

    @ApiOperation("后台查询商品列表")
    @PostMapping("/findAdminProductList")
    public R<AdminProductPageVO> findAdminProductList(@RequestBody AdminProductDTO dto) {
        return productService.findAdminProductList(dto);
    }

    @ApiOperation("搜索店铺的商品")
    @PostMapping("/findAdminStoreProductList")
    public R<ProductPageVO> findAdminStoreProductList(@RequestBody AdminStoreProductDTO adminStoreProductDTO) {
        return productService.findAdminStoreProductList(adminStoreProductDTO);
    }

    @ApiOperation("查询店铺下的分组")
    @PostMapping("/findProductGroupList")
    public R<ProductGroupListVO> findProductGroupList(@RequestBody ProductGroupQueryDTO productGroupQueryDTO) {
        return productService.findProductGroupList(productGroupQueryDTO);
    }

    @ApiOperation(value = "根据分组查询店铺商品",notes = "id传分组id")
    @PostMapping("/findProductListByGroupId")
    public R<ProductPageVO> findProductListByGroupId(@RequestBody ProductListByGroupIdQueryDTO productListByGroupIdQueryDTO) {
        return productService.findProductListByGroupId(productListByGroupIdQueryDTO);
    }

    @ApiOperation("查询快递公司列表")
    @GetMapping(value = {"/findShippingCompanyList/{shippingCompanyName}","/findShippingCompanyList"})
    public R<List<ShippingCompany>> findShippingCompanyList(@PathVariable(value = "shippingCompanyName", required = false) String shippingCompanyName){
        return R.success(shippingCompanyService.findShippingCompanyList(shippingCompanyName));
    }

    @ApiOperation("新增快递公司")
    @PostMapping(value = {"/addShippingCompany"})
    public R<Boolean> addShippingCompany(@RequestBody ShippingCompany shippingCompany){
        return R.success(shippingCompanyService.addShippingCompany(shippingCompany));
    }

    @ApiOperation("更新快递公司")
    @PostMapping(value = {"/updateShippingCompany"})
    public R<Boolean> updateShippingCompany(@RequestBody ShippingCompany shippingCompany){
        return R.success(shippingCompanyService.updateShippingCompany(shippingCompany));
    }

    @ApiOperation("删除快递公司")
    @PostMapping(value = {"/deleteShippingCompany"})
    public R<Boolean> deleteShippingCompany(@RequestBody IdQueryDTO idQueryDTO){
        return R.success(shippingCompanyService.deleteShippingCompany(idQueryDTO));
    }

    @ApiOperation("根据商品列表查询商品信息")
    @PostMapping("/findAdminProductListByIds")
    public R<AdminProductPageVO> findAdminProductListByIds(@RequestBody AdminProductPageQueryDTO dto){
        if(dto==null || StringUtils.isBlank(dto.getProductIds())){
            return R.success(new AdminProductPageVO());
        }
        List<Long> productIdList = new ArrayList<Long>();
        for(String productId:dto.getProductIds().split(",")){
            productIdList.add(Long.valueOf(productId));
        }

        Integer pageIndex = dto.getPageIndex();
        Integer pageSize = dto.getPageSize();

        Integer start = (pageIndex - 1) * pageSize;
        Integer end = start + pageSize;

        if(start >= productIdList.size()){
            return R.success(new AdminProductPageVO());
        }

        if(end > productIdList.size()){
            end = productIdList.size();
        }

        List<Long> subList = productIdList.subList(start,end);

        Integer total = productIdList.size();
        List<AdminProductVO> adminProductList = productService.findAdminProductListByIds(subList);

        AdminProductPageVO pageVO = new AdminProductPageVO();
        pageVO.setTotal(total);
        pageVO.setAdminProductVOList(adminProductList);

        return R.success(pageVO);
    }

    @ApiOperation("更改折扣贡献值分配")
    @PostMapping("/updateProductDiscContri")
    public R<Boolean> updateProductDiscContri(@RequestBody ProductDiscContriConfigUpdateDTO dto) {
        return productService.updateProductDiscContri(dto);
    }

    @ApiOperation("更改贡献值配置")
    @PostMapping("/updateProductContribution")
    public R<Boolean> updateProductContribution(@RequestBody ProductContributionConfigUpdateDTO dto) {
        return productService.updateProductContribution(dto);
    }
}
